/* eslint-disable */
const { Controller } = require('egg');

class UserController extends Controller {
    async index() {
        const { ctx } = this;
        const username = ctx.query.username ? ctx.query.username : '';
        const phone = ctx.query.phone ? ctx.query.phone : ''
        const { pageIndex, pageSize } = ctx.query
        const result = await ctx.service.user.findList(username, phone, pageIndex, pageSize)
        ctx.success(result, '查询用户信息成功')
    }

    //查找某一用户
    async show() {
        const { ctx } = this;
        const id = ctx.params.id;
        const result = await ctx.service.user.findById(id);
        ctx.success(result, `查询id为${id}的用户信息成功`);
    }



    //添加
    async create() {
        const { ctx } = this;
        const user = ctx.request.body;
        const valid = await ctx.service.user.checkUserNameValid(user.username);
        if (valid) {
            const result = await ctx.service.user.add(user);
            ctx.success(result, '添加用户信息成功');
        } else {
            ctx.error(`用户名为${user.username}的用户已存在！`);
        }
    }
    //删除
    async destroy() {
        const { ctx } = this;
        const id = ctx.params.id;
        const result = await ctx.service.user.remove(id);
        if (result) {
            ctx.success(null, "删除用户成功");
        } else {
            ctx.error('删除用户失败');
        }
    }

    //修改
    async update() {
        const { ctx } = this;
        const result = ctx.service.user.modify(ctx.params.id, ctx.request.body)
        if (result) {
            ctx.success(null, "更新用户信息成功")
        } else {
            ctx.error('更新用户信息失败')
        }
    }

    //修改状态
    async changeStatus() {
        const { ctx } = this;
        const { status } = ctx.request.body;
        const id = ctx.params.id;

        const result = await ctx.service.user.changeStatus(id, status);
        if (result) {
            ctx.success(status, '用户状态更新成功');
        } else {
            ctx.error('用户状态更新失败');
        }
    }
}
module.exports = UserController; 